package com.yqkj.zysoft.upms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.project.easy.common.framework.auth.core.IQueryUser;
import  com.project.easy.common.util.dto.UserInfo;
import  com.project.easy.common.util.exception.business.BusinessException;
import  com.project.easy.common.util.string.StringUtil;
import com.yqkj.zysoft.upms.entity.FrameworkDepart;
import com.yqkj.zysoft.upms.entity.FrameworkUser;
import com.yqkj.zysoft.upms.service.IFrameworkDepartService;
import com.yqkj.zysoft.upms.service.IFrameworkUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
 * @author GreatorMan.cool
 * @copyright: Copyright (c) 2020
 * @company: 扬起科技有限公司
 * @date 2021/10/26 22:10
 * @description:用户查询
 */
@Service
public class UpmsQueryUser implements IQueryUser {

    @Autowired
    private IFrameworkUserService frameworkUserService;

    @Autowired
    private IFrameworkDepartService frameworkDepartService;
    /**
     * 根据用户查询信息
     *
     * @param acc 账户
     * @return 返回用户将信息
     */
    @Override
    public UserInfo userInfo(String acc) {
        if (StringUtil.isBlank(acc)) {
            throw new BusinessException("登陆账户不能为空!");
        }
        QueryWrapper<FrameworkUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("password", "acc_no", "status", "id", "username", "depart_id", "depart_code");
        queryWrapper.eq("acc_no", acc);
        FrameworkUser one = frameworkUserService.getOne(queryWrapper);
        if (Objects.isNull(one)) {
            throw new BusinessException("登陆账户非法!");
        }
        UserInfo userInfo = new UserInfo();
        if (Objects.nonNull(one.getDepartId()) && StringUtil.isBlank(one.getDepartCode())) {
            FrameworkDepart frameworkDepart = this.frameworkDepartService.getBaseMapper().selectById(one.getDepartId());
            if (Objects.nonNull(frameworkDepart)) {
                userInfo.setDepartCode(frameworkDepart.getInnerCode());
            }
        } else {
            userInfo.setDepartCode(one.getDepartCode());
        }

        userInfo.setPassword(one.getPassword());
        userInfo.setAcc(one.getAccNo());
        userInfo.setStates(one.getStatus());
        userInfo.setUserId(one.getId());
        userInfo.setUserName(one.getUsername());
        return userInfo;
    }

    @Override
    public int getOrder() {
        return 0;
    }
}
